/*	ResearchTech class definition.
 *
*/

#include "ResearchTech.h"

void ResearchTech::Enter()
{
	DEBUG_PRINT("ResearchTech(%p) %s entering\n", this, type.getName().c_str());

}

void ResearchTech::Execute()
{
	DEBUG_PRINT("ResearchTech(%p) %s executing\n", this, type.getName().c_str());
	if(upgrader)
	{
		BB->SpendGas(type.gasPrice());
		BB->SpendMinerals(type.mineralPrice());
		(upgrader)->research(type);
	}
	control->FinishWithSuccess();
}

void ResearchTech::Exit()
{
	DEBUG_PRINT("ResearchTech(%p) %s exiting\n", this, type.getName().c_str());
	
}

bool ResearchTech::CheckConditions()
{
	DEBUG_PRINT("ResearchTech(%p) %s checking conditions\n", this, type.getName().c_str());
	std::list<BWAPI::Unit*>* upgraders = BB->GetBuildingsByTypeGeneral(type.whatResearches());
	if(upgraders)
	{
		for(std::list<BWAPI::Unit*>::iterator it = upgraders->begin(); it != upgraders->end(); it++)
		{
			if(!(*it)->isResearching() && BWAPI::Broodwar->canResearch(*it,type))
			{
				upgrader = *it;
				break;
			}	
		}
	}
	if(upgrader)
	{
		DEBUG_PRINT("ResearchTech(%p) %s checked conditions and has %d minerals to spend\n", this, type.getName().c_str(), BB->GetMineralsToSpend());
		return true;
	}
	//if(BWAPI::Broodwar->canResearch(NULL,type))//if(BB->isResearch(type))
	//{
	//	
	//}
	return false;
}